/**
 * 
 */
package com.orange.controller.expert;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthExpertService;
import com.orange.handler.AuthExportGroupMapService;
import com.orange.handler.AuthExportGroupService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.ExpertGroupBean;
import com.orange.modules.entity.expert.AuthExpert;
import com.orange.modules.entity.expert.AuthExportGroupMap;
import com.orange.modules.entity.expert.AuthGroup;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.util.ReturnCode;

/**
 * @author gaogs
 *
 * 2016-11-16
 */
@Controller
@RequestMapping("/expertGroup")
public class AuthExportGroupController extends BaseController {

	private static final Logger logger = Logger.getLogger(AuthExportGroupController.class);
	
	@Autowired
	private AuthExportGroupService authExportGroupService;
	
	@Autowired
	private AuthExportGroupMapService authExportGroupMapService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	@Autowired
	private AuthExpertService authExpertService;
	
	/**
	 * 查询未绑定现场考察专家组的所有机构
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryUnAuthSchoolList" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryUnAuthSchoolList(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			List<AuthSchool>ls=authSchoolService.queryUnAuthSchoolList();
			return toResponse(ls,ReturnCode.SUCCESS,"查询未绑定现场考察专家组的所有机构成功");
		} catch (Exception e) {
			logger.info("查询未绑定现场考察专家组的所有机构失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询未绑定现场考察专家组的所有机构失败");
		}
		
	}
	
	
	/**
	 * 创建现场考察专家组
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/saveAuthExpertGroup_review" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthExpertGroup_review(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			if(StringUtils.isBlank(map.get("schoolId"))||StringUtils.isBlank(map.get("name"))){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(map.get("schoolId"));
			if(null==school){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"学校信息不存在");
			}
			if(!"0".equals(school.getFpexpert())){
				return toResponse(null,ReturnCode.SCHOOL_HAVE_COLLEC,"该学校已绑定现场考察专家组,不可重复绑定");
			}
			school.setFpexpert("1");
			authSchoolService.updateAuthSchoolInfo(school);
			
			AuthGroup a=new AuthGroup();
			a.setName(map.get("name"));
			a.setState("0");
			a.setType("1");
			a.setSchoolId(map.get("schoolId"));
			String nowTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			a.setCreateTime(nowTime);
			a.setUpdateTime(nowTime);
			authExportGroupService.saveAuthExportGroupInfo(a);
			return toResponse(null,ReturnCode.SUCCESS,"创建专家组成功");
		} catch (Exception e) {
			logger.info("创建专家组失败",e);
			return toResponse(null,ReturnCode.FAIL,"创建专家组失败");
		}
	}
	
	/**
	 * 设置现场考察专家组状态
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/setAuthExpertGroupState" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> setAuthExpertGroupState(HttpServletRequest request,@RequestParam String groupId,@RequestParam String state) throws SQLException{
		
		try {
			if(StringUtils.isBlank(state)||StringUtils.isBlank(groupId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			
			AuthGroup a=authExportGroupService.queryAuthExportGroupInfo(groupId);
			if(null==a){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该组不存在");
			}
			if("1".equals(state)){
				if(!"0".equals(a.getState())){
					return toResponse(null,ReturnCode.FAIL,"别瞎设置状态");
				}
			}else if("3".equals(state)){
				if(!"1".equals(a.getState())){
					return toResponse(null,ReturnCode.FAIL,"别瞎设置状态");
				}
				//根据专家组id查询所有绑定的专家
				List<AuthExportGroupMap> ls=authExportGroupMapService.queryAuthExportGroupMapByGroupId(a.getPid());
				List<AuthExpert> list=new ArrayList<AuthExpert>();
				if(ls.size()>0){
					for(AuthExportGroupMap aa:ls){
						AuthExpert ae=authExpertService.queryAuthExpertInfoById(aa.getExportId());
						if(null==ae){
							return toResponse(null, ReturnCode.USERINFO_NOT_EXIST, "专家信息不存在");
						}
						ae.setStatus("0");//绑定的专家解绑
						list.add(ae);
					}
				}
				authExpertService.saveAuthExpertInfos(list);
				
			}else{
				return toResponse(null,ReturnCode.FAIL,"别瞎设置状态");
			}
			a.setState(state);
			authExportGroupService.saveAuthExportGroupInfo(a);
			return toResponse(null,ReturnCode.SUCCESS,"设置现场考察专家组状态成功");
		} catch (Exception e) {
			logger.info("设置现场考察专家组状态失败",e);
			return toResponse(null,ReturnCode.FAIL,"设置现场考察专家组状态失败");
		}
	}
	
	/**
	 * 根据id查询现场考察专家组
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthExportGroupInfoByid" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthExportGroupInfoByid(HttpServletRequest request,@RequestParam String groupId) throws SQLException{
		
		try {
			if(StringUtils.isBlank(groupId)){
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthGroup a=authExportGroupService.queryAuthExportGroupInfo(groupId);
			if(null==a){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该组不存在");
			}
			AuthSchool as=authSchoolService.queryAuthSchoolInfoById(a.getSchoolId());
			if(null==as){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"组内学校不存在");
			}
			a.setSchoolName(as.getSchoolName());
			return toResponse(a,ReturnCode.SUCCESS,"根据id查询现场考察专家组状态成功");
		} catch (Exception e) {
			logger.info("根据id查询现场考察专家组状态失败",e);
			return toResponse(null,ReturnCode.FAIL,"根据id查询现场考察专家组状态失败");
		}
	}
	
	/**
	 * 删除现场考察专家组
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteAuthExpertGroup", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> deleteAuthExpertGroup(HttpServletRequest request, @RequestParam String pid)
			throws SQLException {
		try {
			if (StringUtils.isBlank(pid)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"删除Id不能为空");
			}
			AuthGroup ag = authExportGroupService.queryAuthExportGroupInfo(pid);
			if(null==ag){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该现场考察专家组信息不存在");
			}
			if(!"0".equals(ag.getState())){
				return toResponse(null,ReturnCode.EXPERT_GROUP_HAVE_USED,"该专家组已启用不可删除");
			}
			
			AuthSchool school=authSchoolService.queryAuthSchoolInfoById(ag.getSchoolId());
			if(null==school){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"学校信息不存在");
			}
			school.setFpexpert("0");
			authSchoolService.updateAuthSchoolInfo(school);
			
			//根据专家组id查询所有绑定的专家
			List<AuthExportGroupMap> ls=authExportGroupMapService.queryAuthExportGroupMapByGroupId(pid);
			List<AuthExpert> list=new ArrayList<AuthExpert>();
			if(ls.size()>0){
				for(AuthExportGroupMap a:ls){
					AuthExpert ae=authExpertService.queryAuthExpertInfoById(a.getExportId());
					if(null==ae){
						return toResponse(null, ReturnCode.USERINFO_NOT_EXIST, "专家信息不存在");
					}
					ae.setStatus("0");//绑定的专家解绑
					list.add(ae);
				}
			}
			authExpertService.saveAuthExpertInfos(list);
			authExportGroupService.deleteAuhExportGroupInfo(ag.getPid());//删除组

			return toResponse(null, ReturnCode.SUCCESS, "删除现场考察专家组成功");
		} catch (Exception e) {
			logger.info("删除现场考察专家组失败",e);
			return toResponse(null, ReturnCode.FAIL, "删除现场考察专家组失败");
		}
	}

	
	
	/**
	 * 修改现场考察专家组信息
	 */
	@RequestMapping(value = "/updateAuthExpertGroup" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateAuthExpert(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try{
			String pid = map.get("pid");
			String schoolId = map.get("schoolId");
			String name = map.get("name");
			AuthGroup ag = authExportGroupService.queryAuthExportGroupInfo(pid);
			if(ag == null){
				return toResponse(null,ReturnCode.FAIL,"无该现场考察专家组信息");
			}
			if(!"0".equals(ag.getState())){
				return toResponse(null,ReturnCode.EXPERT_GROUP_HAVE_USED,"该专家组已启用不可修改");
			}
			if(null!=schoolId){
				AuthSchool oriSchool=authSchoolService.queryAuthSchoolInfoById(ag.getSchoolId());
				if(null==oriSchool){
					return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"学校信息不存在");
				}
				
				AuthSchool newSchool=authSchoolService.queryAuthSchoolInfoById(schoolId);
				if(null==newSchool){
					return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"学校信息不存在");
				}
				
				oriSchool.setFpexpert("0");
				authSchoolService.updateAuthSchoolInfo(oriSchool);//原学校状态修改为 未绑定
				
				newSchool.setFpexpert("1");
				authSchoolService.updateAuthSchoolInfo(newSchool);//新绑定学校状态修改
				
				ag.setSchoolId(schoolId);
			}
			if(null!=name){
				ag.setName(name);
			}
			String nowTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			ag.setUpdateTime(nowTime);
			authExportGroupService.saveAuthExportGroupInfo(ag);
			return toResponse(ag,ReturnCode.SUCCESS,"修改现场考察专家组信息成功");
		}catch(Exception e){
			logger.info("修改现场考察专家组信息失败",e);
			return toResponse(null,ReturnCode.FAIL,"修改现场考察专家组信息失败");
		}
	}
	
	/**
	 * 分页查询现场考察专家组
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthGroupPage_review", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthGroupPage(HttpServletRequest request, @RequestBody Map<String, String> map)throws SQLException {
		
		try {
			String pageNumber = map.get("pageNumber");
			String pageSize = map.get("pageSize");
			if (StringUtils.isBlank(pageNumber))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "数量不能为空!");
			if (StringUtils.isBlank(pageSize))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "页数不能为空!");
			
			Page<AuthGroup> page = authExportGroupService.queryAuthGroupPage(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询现场考察专家组成功");
		} catch (Exception e) {
			logger.info("分页查询现场考察专家组失败", e);
			return toResponse(null, ReturnCode.FAIL, "分页查询现场考察专家组失败");
		}
		
	}

	/**
	 * 查询未绑定现场考察专家组的所有专家
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryUnAuthExpertList" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryUnAuthExpertList(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try {
			
//			List<AuthGroup> lg = authExportGroupMapService.queryGroupIdByState("3");
//			List<AuthExportGroupMap> le = new ArrayList<AuthExportGroupMap>();
//			AuthExpert expert = new AuthExpert();
//			if(null != lg){
//				for (int i = 0; i < lg.size(); i++) {
//					le = authExportGroupMapService.queryExpertIdByGroupId(lg.get(i).getPid());
//					if(null != le){
//						for (int j = 0; j < le.size(); j++) {
//							expert = authExpertService.queryAuthExpertInfoById(le.get(j).getExportId());
//							expert.setStatus("0");
//							authExpertService.updateAuthExpert(expert);
//						}
//					}
//				}
//			}
			
			List<AuthExpert> ls=authExpertService.queryUnAuthExpertList();
			return toResponse(ls,ReturnCode.SUCCESS,"查询未绑定现场考察专家组的所有专家成功");
		} catch (Exception e) {
			logger.info("查询未绑定现场考察专家组的所有专家失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询未绑定现场考察专家组的所有专家失败");
		}
		
	}
	
	/**
	 * 现场考察专家组-添加组员
	 */
	@RequestMapping(value = "/addAuthExportGroupMap" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> addAuthExportGroupMap(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try{
			String exportIds = map.get("exportIds");
			String groupId = map.get("groupId");
			
			
			
			if(StringUtils.isBlank(exportIds)|| StringUtils.isBlank(groupId))
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家组id或者专家为空");
			
			List<AuthExportGroupMap> list=new ArrayList<AuthExportGroupMap>();
			List<AuthExpert> as=new ArrayList<AuthExpert>();
			String [] strs=exportIds.split(",");
			if(strs.length>0){
				String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				for(int i=0 ;i<strs.length ;i++){
					// 设置专家状态为绑定状态
					AuthExpert ae=authExpertService.queryAuthExpertInfoById(strs[i]);
					if(null==ae){
						return toResponse(null, ReturnCode.USERINFO_NOT_EXIST, "专家信息不存在");
					}
					if("1".equals(ae.getStatus())){
						return toResponse(null, ReturnCode.SCHOOL_HAVE_GROUP, "专家已绑定现场考察专家组");
					}
					ae.setStatus("1");
					as.add(ae);
					
					AuthExportGroupMap a=new AuthExportGroupMap();
					a.setCreateTime(nowTime);
					a.setExportId(strs[i]);
					a.setGroupId(groupId);
					a.setIdentitys("0");
					a.setType("1");
					list.add(a);
				}
			}
			authExportGroupMapService.addAuthExportGroupMapList(list);
			authExpertService.saveAuthExpertInfos(as);
			return toResponse(null, ReturnCode.SUCCESS, "现场考察专家组-添加组员成功");
		}catch(Exception e){
			logger.info("现场考察专家组-添加组员失败", e);
			return toResponse(null,ReturnCode.FAIL,"现场考察专家组-添加组员失败");
		}
	}
	
	/**
	 * 删除现场考察专家组组员
	 * 
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteAuthExpertGroupMap", produces = MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> deleteAuthExpertGroupMap(HttpServletRequest request, @RequestParam String pid)
			throws SQLException {
		try {
			if (StringUtils.isBlank(pid)) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"删除Id不能为空");
			}
			AuthExportGroupMap aa=authExportGroupMapService.queryAuthExportGroupMapById(pid);
			if(null==aa){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该组员记录不存在");
			}
			AuthGroup ag = authExportGroupService.queryAuthExportGroupInfo(aa.getGroupId());
			if(null==ag){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该现场考察专家组信息不存在");
			}
			if(!"0".equals(ag.getState())){
				return toResponse(null,ReturnCode.EXPERT_GROUP_HAVE_USED,"该现场考察专家组已启用不可删除组员");
			}
			AuthExpert ae=authExpertService.queryAuthExpertInfoById(aa.getExportId());
			if(null==ae){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该专家记录不存在");
			}
			ae.setStatus("0");
			authExpertService.updateAuthExpert(ae);
			
			authExportGroupMapService.deleteAuthExportGroupMapById(pid);
			return toResponse(null, ReturnCode.SUCCESS, "删除现场考察专家组成功");
		} catch (Exception e) {
			logger.info("删除现场考察专家组失败",e);
			return toResponse(null, ReturnCode.FAIL, "删除现场考察专家组失败");
		}
	}

	
	
	/**
	 *   分页查看现场考察专家组内 所有专家
	 */
	@RequestMapping(value = "/queryAuthExportGroupMapPage" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthExportGroupMapPage(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		
		try{
			String pageSize = map.get("pageSize");
			String pageNumber = map.get("pageNumber");
			String groupId = map.get("groupId");
			
			if(StringUtils.isBlank(pageSize)||StringUtils.isBlank(pageNumber)||StringUtils.isBlank(groupId)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			Page<AuthExportGroupMap> ls=authExportGroupMapService.queryAuthExportGroupMapPage(map);
			return toResponse(ls, ReturnCode.SUCCESS, "分页查看现场考察专家组内 所有专家成功");
		}catch(Exception e){
			logger.info("分页查看现场考察专家组内 所有专家失败", e);
			return toResponse(null,ReturnCode.FAIL,"分页查看现场考察专家组内 所有专家失败");
		}
	}
	
	/**
	 *   设置现场考察专家组 组员身份
	 */
	@RequestMapping(value = "/setAuthExportIdentity" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> setAuthExportIdentity(HttpServletRequest request,@RequestParam String pid,@RequestParam String identity) throws SQLException{
		
		try{
			if(StringUtils.isBlank(pid)||StringUtils.isBlank(identity)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			AuthExportGroupMap aa=authExportGroupMapService.queryAuthExportGroupMapById(pid);
			if(null==aa){
				return toResponse(null,ReturnCode.USERINFO_NOT_EXIST,"该组员关系不存在");
			}
			
			if(!"0".equals(identity)&&!"1".equals(identity)&&!"2".equals(identity)&&!"3".equals(identity)){
				return toResponse(null,ReturnCode.FAIL,"身份别瞎填！");
			}
			AuthGroup ag = authExportGroupService.queryAuthExportGroupInfo(aa.getGroupId());
			if(null==ag){
				return toResponse(null, ReturnCode.USERINFO_NOT_EXIST,"该现场考察专家组信息不存在");
			}
			if(!"0".equals(ag.getState())){
				return toResponse(null,ReturnCode.EXPERT_GROUP_HAVE_USED,"该现场考察专家组已启用不可再改变组员身份");
			}
			aa.setIdentitys(identity);
			if(!"0".equals(identity)){
				AuthExportGroupMap a1=authExportGroupMapService.queryAuthExportGroupMapByIdentity(aa.getGroupId(), identity);
				if(null!=a1){
					a1.setIdentitys("0");
					authExportGroupMapService.saveAuthExportGroupMap(a1);
				}
			}
			authExportGroupMapService.saveAuthExportGroupMap(aa);
			
			return toResponse(null, ReturnCode.SUCCESS, "设置现场考察专家组 组员身份成功");
		}catch(Exception e){
			logger.info("设置现场考察专家组 组员身份失败", e);
			return toResponse(null,ReturnCode.FAIL,"设置现场考察专家组 组员身份失败");
		}
	}
	
	
	
	/****************************************组长汇总查看**********************************************/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
